softaction= function(){
	return {
	installSoft:function(objcode){
		 var sql="SELECT XMLID, PRO_KEY, PRO_VALUE, PARENTCODE, PRO_NAME, REMARK,ITEMCODE FROM metaobj_property where xmlid='"+objcode+"'";
		 var softPropertyStore=  new AI.JsonStore({
                sql:sql,
                key:"XMLID,PRO_KEY",
                pageSize:-1,
                table:"METAOBJ_PROPERTY"  
      });
     //检查共有模型表
     var reftabsRecord = softPropertyStore.getRecordByKey(objcode+',reftabs');
     var refCkeckResult = this.checkExistTab(reftabsRecord);
     if(!refCkeckResult.success){
     	 alert(refCkeckResult.msg);
     	 return false;
     }
     //执行sql脚本
     var scriptRecord = softPropertyStore.getRecordByKey(objcode+',install_script');
     var scriptResult = this.executeScript(scriptRecord);
     if(!scriptResult.success){
     	 alert(scriptResult.msg);
     	 return false;
     }
     //检查私有模型表
     var selftabsRecord = softPropertyStore.getRecordByKey(objcode+',selftabs');
     var selfCkeckResult = this.checkExistTab(selftabsRecord);
     if(!selfCkeckResult.success){
     	 alert(selfCkeckResult.msg);
     	 return false;
     }
     //生成菜单方式
     var prodmenuRecord = softPropertyStore.getRecordByKey(objcode+',prodmenu');
     this.buildMenuModel(prodmenuRecord);
     
     //生成右键菜单
     var contextMenuRecord = softPropertyStore.getRecordByKey(objcode+',contextMenu');
     this.buildContextMenu(contextMenuRecord);
     //生成工具栏菜单
     var toolbarRecord = softPropertyStore.getRecordByKey(objcode+',toolbar');
     this.buildToolBar(toolbarRecord);
	},
	checkExistTab: function(tabRecord){
		var result ={};
		if(!tabRecord||!tabRecord.get("PRO_VALUE")){
			result.success=true;
			result.msg="没有脚本信息";
			return result;
		}
		var checkTabArr = tabRecord.get("PRO_VALUE").split(",");
		var noExistTab ="";
	  for(var i=0;i<checkTabArr.length;i++){
	  	var _sql = "desc "+checkTabArr[i];
	  	var checkResult = ai.executeSQL(_sql);
	  	if(!checkResult.success){
	  		result.success=false;
	  		noExistTab+=checkTabArr[i]+",";
	  	}
	  }
	  if(noExistTab&&noExistTab.length>0){
	  	noExistTab = noExistTab.substring(0,noExistTab.length-1);
	  	result.msg = noExistTab+"这些表不存在,请检查!!";
	  }else{
	  	result.success=true;
	  	result.msg="检验通过";
	  }
	  return result;
	},
	executeScript:function(scriptRecord){
		var result ={};
		if(!scriptRecord||!scriptRecord.get("PRO_VALUE")){
			result.success=true;
			result.msg="没有执行的脚本";
			return result;
		}
		var scriptValArr = scriptRecord.get("PRO_VALUE").split(";");
		var MultiSql="";
		for(var i=0;i<scriptValArr.length;i++){
			if(i==0){
				MultiSql+='[\"'+scriptValArr[i]+'\"';
			}else if (i==scriptValArr.length){
				MultiSql+=',\"'+scriptValArr[i]+'\"]';
			}else{
				MultiSql+=',\"'+scriptValArr[i]+'\"';
			}
		}
		result = ai.executeMultiSql(MultiSql);
		return result;
	},
	buildMenuModel:function(menuModelRecord){
		if(!menuModelRecord||!menuModelRecord.get("PRO_VALUE")) return true;
		var modelJson = JSON.parse(menuModelRecord.get("PRO_VALUE"));
		var metaModelStore = new AI.JsonStore({
				sql:"select * from metamodel where 1=2",
      	key:"MODELCODE",
      	pageSize:-1,
        table:"METAMODEL" 
		});
		var addMenuCode="";
		if(modelJson instanceof Array){
			for(var i=0;i<modelJson.length;i++){
				var modelArrObj = modelJson[i];
				var newArrRec=metaModelStore.getNewRecord();
				$.each(modelArrObj, function(key, value) {
					newArrRec.set(key.toUpperCase(),value);
				});
				metaModelStore.add(newArrRec);
				addMenuCode +="'"+newArrRec.get("MODELCODE")+"',";
			}
		}else{
			var newRec=metaModelStore.getNewRecord();
			$.each(modelJson, function(key, value) {
					newRec.set(key.toUpperCase(),value);
			});
			metaModelStore.add(newRec);
			addMenuCode +="'"+newArrRec.get("MODELCODE")+"',";
		}
		if(addMenuCode&&addMenuCode.length>0){
			ai.executeSQL("delete from metamodel where modelcode in("+addMenuCode.substring(0,addMenuCode.length-1)+")");
		}
		metaModelStore.commit();
		return true;
	},
	buildContextMenu:function(contextMenuRecord){
		if(!contextMenuRecord||!contextMenuRecord.get("PRO_VALUE")) return true;
		var menuJson = JSON.parse(contextMenuRecord.get("PRO_VALUE"));
		ai.executeSQL("delete from metasoft_action where provider='"+contextMenuRecord.get("XMLID")+"' and dockplace='rightmenu'");
		var contextMenuActionStore = new AI.JsonStore({
				sql:"select * from metasoft_action where 1=2",
        key:"PROVIDER,DOCKPLACE,ACTCODE",
        pageSize:-1,
        table:"METASOFT_ACTION"  
		});
		
		if(menuJson instanceof Array){
			for(var i =0;i<menuJson.length;i++){
				var menuArrObj = menuJson[i];
				var newArrRec=contextMenuActionStore.getNewRecord();
				newArrRec.set("ACTTYPE","action");
				newArrRec.set("PROVIDER",contextMenuRecord.get("XMLID"));
				newArrRec.set("DOCKPLACE","rightmenu");
				$.each(menuArrObj, function(key, value) {
					newArrRec.set(key.toUpperCase(),value);
				});
				contextMenuActionStore.add(newArrRec);
			}	
		}else{
			var newRec=contextMenuActionStore.getNewRecord();
			newRec.set("ACTTYPE","action");
			newRec.set("PROVIDER",contextMenuRecord.get("XMLID"));
			newRec.set("DOCKPLACE","rightmenu");
			$.each(menuJson, function(key, value) {
					newRec.set(key.toUpperCase(),value);
			});
			contextMenuActionStore.add(newRec);
		}
		contextMenuActionStore.commit();
		return true;
	},
	buildToolBar: function(toolbarRecord){
		if(!toolbarRecord||!toolbarRecord.get("PRO_VALUE")) return true;
		var toolBarJson = JSON.parse(toolbarRecord.get("PRO_VALUE"));
		ai.executeSQL("delete from metasoft_action where provider='"+toolbarRecord.get("XMLID")+"' and dockplace='toolbar'");
		var toolBarActionStore = new AI.JsonStore({
				sql:"select * from metasoft_action where 1=2",
        key:"PROVIDER,DOCKPLACE,ACTCODE",
        pageSize:-1,
        table:"METASOFT_ACTION"  
		});
		
		if(toolBarJson instanceof Array){
			for(var i =0;i<toolBarJson.length;i++){
				var toolBarArrObj = toolBarJson[i];
				var newArrRec=toolBarActionStore.getNewRecord();
				newArrRec.set("ACTTYPE","action");
				newArrRec.set("PROVIDER",toolbarRecord.get("XMLID"));
				newArrRec.set("DOCKPLACE","toolbar");
				$.each(toolBarArrObj, function(key, value) {
					newArrRec.set(key.toUpperCase(),value);
				});
				toolBarActionStore.add(newArrRec);
			}	
		}else{
			var newRec=toolBarActionStore.getNewRecord();
			newRec.set("ACTTYPE","action");
			newRec.set("PROVIDER",toolbarRecord.get("XMLID"));
			newRec.set("DOCKPLACE","toolbar");
			$.each(toolBarJson, function(key, value) {
					newRec.set(key.toUpperCase(),value);
			});
			toolBarActionStore.add(newRec);
		}
		toolBarActionStore.commit();
		return true;
	}
 }
}();
